package com.jxd.project.wzgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.jxd.project.absence.model.Dept;
import com.jxd.project.absence.service.IDeptService;
import com.jxd.project.menu.model.Application;
import com.jxd.project.menu.model.ApplicationGoods;
import com.jxd.project.menu.model.Log;
import com.jxd.project.wzgl.dto.ApplicationDTO;
import com.jxd.project.wzgl.dto.QueryApplicationDTO;
import com.jxd.project.wzgl.service.IApplicationGoodsService;
import com.jxd.project.wzgl.service.IApplicationService;
import com.jxd.project.wzgl.service.impl.LogServiceImpl;
import com.jxd.project.wzgl.vo.HistoryBRGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ApplicationController
 * @Description TODO
 * @Author 刘成
 * @Date 2025/10/1 20:21
 * @Version 1.0
 */
@RestController
@RequestMapping("/lc")
public class ApplicationController {
    @Autowired
    @Qualifier("applicationGoodsServiceImpl")
    private IApplicationGoodsService applicationGoodsService;
    @Autowired
    @Qualifier("applicationServiceImpl")
    private IApplicationService applicationService;
    @Autowired
    @Qualifier("deptServiceImpl")
    private IDeptService deptService;
    @Autowired
    @Qualifier("logServiceImpl")
    private LogServiceImpl logService;

    /**
     * 个人借用申请待办
     * @param empNo
     * @return
     */
    @RequestMapping("/getPersonalBorrowBackLog")
    public String getPersonalBorrowBackLog(int empNo){
        QueryWrapper<Application> qw = new QueryWrapper<>();
        qw.eq("applicationempno",empNo);
        qw.eq("applicationname","借用申请");
        qw.isNotNull("bannedreason");
        qw.eq("checktype",1);
        return applicationService.list(qw).size() + "";
    }

    /**
     * 个人归还申请待办
     * @param empNo
     * @return
     */
    @RequestMapping("/getPersonalReturnBackLog")
    public String getPersonalReturnBackLog(int empNo){
        QueryWrapper<Application> qw = new QueryWrapper<>();
        qw.eq("applicationempno",empNo);
        qw.eq("applicationname","归还申请");
        qw.isNotNull("bannedreason");
        qw.eq("checktype",1);
        return applicationService.list(qw).size() + "";
    }

    /**
     * 部门借用审核待办
     * @param deptNo
     * @return
     */
    @RequestMapping("/getDeptBorrowBackLog")
    public String getDeptBorrowBackLog(int deptNo){
        QueryWrapper<Application> qw = new QueryWrapper<>();
        qw.eq("deptno",deptNo);
        qw.eq("applicationname","借用申请");
        qw.isNotNull("bannedreason");
        qw.eq("checktype",2);
        return applicationService.list(qw).size() + "";
    }

    /**
     * 仓库借用审核待办
     * @param warehouseId
     * @return
     */
    @RequestMapping("/getWarehouseBorrowBackLog")
    public String getWarehouseBorrowBackLog(int warehouseId){
        QueryWrapper<Application> qw = new QueryWrapper<>();
        qw.eq("warehouseid",warehouseId);
        qw.eq("applicationname","借用申请");
        qw.isNotNull("bannedreason");
        qw.eq("checktype",3);
        return applicationService.list(qw).size() + "";
    }

    /**
     * 仓库归还审核待办
     * @param warehouseId
     * @return
     */
    @RequestMapping("/getWarehouseReturnBackLog")
    public String getWarehouseReturnBackLog(int warehouseId){
        QueryWrapper<Application> qw = new QueryWrapper<>();
        qw.eq("warehouseid",warehouseId);
        qw.eq("applicationname","归还申请");
        qw.isNotNull("bannedreason");
        qw.eq("checktype",3);
        return applicationService.list(qw).size() + "";
    }

    /**
     * 双重身份借用审核待办
     * @param empNo
     * @return
     */
    @RequestMapping("/getDoubleIdentityBorrowBackLog")
    public String getDoubleIdentityBorrowBackLog(int empNo){
        return applicationService.getDoubleIdentityBorrowBackLogCount(empNo) + "";

    }

    @RequestMapping("/addApplication")
    public Map<String, Object> addApplication(@RequestBody  ApplicationDTO application){
        return applicationService.addApplication(application);
    }

    @RequestMapping("/updateApplication")
    public String updateApplication(@RequestBody ApplicationDTO application){
        boolean result =applicationService.updateApplication(application);
        return result ? "success" : "fail";
    }

    @RequestMapping("/updateApplicationStatus")
    public String updateApplicationStatus(@RequestBody ApplicationDTO application){
        boolean result = applicationService.updateApplicationStatus(application);
        return result ? "success" : "fail";
    }

    @RequestMapping("/deleteApplication")
    public String deleteApplication(@RequestBody List<Integer> applicationIDs){
        boolean result = applicationService.deleteApplication(applicationIDs);
        return result ? "success" : "fail";
    }

    @RequestMapping("/getApplicationWithQueryApplicationDTO")
    public Map<String,Object> getApplicationWithQueryApplicationDTO(@RequestBody QueryApplicationDTO queryApplicationDTO){
        return applicationService.getApplicationWithQueryApplicationDTO(queryApplicationDTO);
    }

    @RequestMapping("/getApplicationGoods")
    public List<ApplicationGoods> getApplicationGoods(int applicationId) {
        return applicationGoodsService.getWithApplicationId(applicationId);
    }

    @RequestMapping("/getNotReturn")
    public List<ApplicationGoods> getNotReturn(@RequestParam("empNo") int employeeId,@RequestParam("warehouseId") int warehouseId){
        return applicationGoodsService.getNotReturnGoods(employeeId,warehouseId);
    }

    //前端流程图格式待调整
    @RequestMapping("/queryLogs")
    public List<Log> getLogs(int applicationId){
        return logService.selectLogWithApplicationId(applicationId);
    }

    @RequestMapping("/getHistoryBRGoods")
    public List<HistoryBRGoods> getHistoryBRGoods(int empNo){
        return applicationGoodsService.getHistoryBRGoods(empNo);
    }

    @RequestMapping("/getDepts")
    public List<Dept> getDepts(){
        return deptService.list();
    }

    @RequestMapping("/getDeptName")
    public String getDeptName(int deptNo){
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq("deptno",deptNo);
        Dept dept = deptService.getOne(qw);
        return dept == null ? null : dept.getDname();
    }


}
